package com.cskaoyan.market.service.admin.impl;

import com.cskaoyan.market.db.mapper.MarketOrderGoodsMapper;
import com.cskaoyan.market.db.mapper.MarketOrderMapper;
import com.cskaoyan.market.db.mapper.MarketUserMapper;
import com.cskaoyan.market.service.admin.MarketStatService;
import com.cskaoyan.market.vo.StatGoodVo;
import com.cskaoyan.market.vo.StatOrderVo;
import com.cskaoyan.market.vo.StatUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @ClassName MarketStatusServiceImpl
 * @Description TODO
 * @Author MZ
 * @Date 2023/5/9 21:22
 */
@Service
public class MarketStatServiceImpl implements MarketStatService {
    @Autowired
    MarketUserMapper userMapper;
    @Autowired
    MarketOrderMapper orderMapper;
    @Autowired
    MarketOrderGoodsMapper goodsMapper;

    @Override
    public List<StatUserVo> statUsers() {

        List<StatUserVo> statList = userMapper.countByDateTime();

        return statList;
    }

    @Override
    public List<StatOrderVo> statOrders() {
        List<StatOrderVo> statLists = orderMapper.countOrdersByDate();

        for (StatOrderVo statList : statLists) {
            // 统计amount 订单总额
            BigDecimal amount = orderMapper.sumOrderPriceByDate(statList.getDay());
            statList.setAmount(amount);
            // 统计customers 用户
            Integer customers = orderMapper.countCustomersByDate(statList.getDay());
            statList.setCustomers(customers);
            // 统计pcr 客单价（平均交易额）
            BigDecimal pcr = orderMapper.avgOrderPriceByDate(statList.getDay());
            statList.setPcr(pcr);
        }
        return statLists;
    }

    @Override
    public List<StatGoodVo> statGoods() {

        List<StatGoodVo> statLists = goodsMapper.countOrdersByDate();
        for (StatGoodVo statList : statLists) {
            // 统计amount 订单总额
            BigDecimal amount = goodsMapper.sumPriceByDate(statList.getDay());
            statList.setAmount(amount);
            // 统计
            Integer products = goodsMapper.sumNumberByDate(statList.getDay());
            statList.setProducts(products);
        }
        return statLists;
    }
}
